package com.drug.lib.view.PopupWindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.drug.lib.util.DLog;

public abstract class BasePopupWindow extends PopupWindow {
    //    private int popupHeight, popupWidth;
    public Context mContext;

    public BasePopupWindow(@NonNull Context context) {
        super(context);
        this.mContext = context;
        setOutsideTouchable(isOutsideTouch());
//        setPopupWindowSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(getContentLayout());
        initView();
    }

    protected abstract boolean isOutsideTouch();

    protected abstract View getContentLayout();

    protected abstract void initView();


    protected void setPopupWindowSize(int width, int hight) {
        setWidth(width);
        setHeight(hight);
    }

    /**
     * 计算出PopUpWindow显示的高度
     *
     * @param context     Context
     * @param popupWindow PopupWindow对象
     * @return PopUpWindow的高度
     */
    private int calculatePopWindowHeight(Context context, PopupWindow popupWindow) {
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        if (popupWindow.getHeight() > 0) {
            return popupWindow.getHeight();
        }
        int popupWindowWidth = popupWindow.getWidth();
        int measureWidthParams = 0;
        if (popupWindowWidth > 0) {
            measureWidthParams = View.MeasureSpec.makeMeasureSpec(popupWindowWidth, View.MeasureSpec.EXACTLY);
        }
        if (popupWindowWidth == WindowManager.LayoutParams.MATCH_PARENT) {
            measureWidthParams = View.MeasureSpec.makeMeasureSpec(screenWidth, View.MeasureSpec.EXACTLY);
        }
        if (popupWindowWidth == WindowManager.LayoutParams.WRAP_CONTENT) {
            popupWindow.setWidth(screenWidth / 2);
            measureWidthParams = View.MeasureSpec.makeMeasureSpec(screenWidth / 2, View.MeasureSpec.EXACTLY);
        }
        popupWindow.getContentView().measure(measureWidthParams, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        //获取自身的长宽高
        int popupHeight = popupWindow.getContentView().getMeasuredHeight();
//        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//        int  popupHeight = getContentView().getMeasuredHeight();
//        int  popupWidth = getContentView().getMeasuredWidth();
        DLog.d("============popupHeight================" + popupHeight);
        return popupHeight;
    }

    /**
     * 计算出参考View的高度
     *
     * @param anchorView 参考view对象
     * @return 参考View的高度
     */
    private int calculateViewHeight(View anchorView) {
        return anchorView.getBottom() - anchorView.getTop();
    }

    /**
     * 显示PopUpWindow在参考布局之上
     *
     * @param context    Context
     * @param anchorView 参考布局
     */

    public void showAsAbove(Context context, View anchorView, PopupWindow popupWindow) {
        int popupWindowHeight = calculatePopWindowHeight(context, popupWindow);
        int anchorViewHeight = calculateViewHeight(anchorView);
        //实际需要偏移的高度为参考view的高度加上PopUpWindow的高度
        int yOffset = -(popupWindowHeight + anchorViewHeight);
        this.showAsDropDown(anchorView, 0, yOffset);
    }

    /**
     * 显示PopUpWindow在参考布局之下
     *
     * @param anchorView 参考布局
     */
    public void showAsBelow(View anchorView) {
        showAsDropDown(anchorView, 0, 0);
    }

    /**
     * 显示PopUpWindow在参考布局之下
     *
     * @param anchorView 参考布局
     */
    public void showAsBelow(View anchorView, int xoff, int yoff) {
        showAsDropDown(anchorView, xoff, yoff);
    }
}
